/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.fatecpg.ads.es3.tp.persistence;

import java.awt.Component;
import javax.swing.JTextField;
import java.awt.Panel;
import java.text.DecimalFormat;
import javax.swing.JPanel;

/**
 *
 * @author Homer
 */
public class ControleCampos {

    public void setDesabilitaCampos(JPanel name) {
        for (Component c : name.getComponents()) {
            if (c instanceof JTextField) {
                JTextField tx = (JTextField) c;
                tx.setEnabled(false);
            }
            for (Component a : name.getComponents()) {
                if (a instanceof JTextField) {
                    JTextField tx = (JTextField) a;
                    tx.setEnabled(false);
                }
            }
        }

    }

    public void setHabilitaCampos(JPanel name) {

        //inserindo valores padrões nos txts de débitos
        for (Component c : name.getComponents()) {
            if (c instanceof JTextField) {
                JTextField tx = (JTextField) c;
                tx.setEnabled(true);
            }
            for (Component a : name.getComponents()) {
                if (a instanceof JTextField) {
                    JTextField tx = (JTextField) a;
                    tx.setEnabled(true);
                }
            }
        }
    }

    public void setValoresTxt(JPanel name) {


        for (Component c : name.getComponents()) {
            if (c instanceof JTextField) {
                JTextField tx = (JTextField) c;
                tx.setText("0,00");
            }
            for (Component a : name.getComponents()) {
                if (a instanceof JTextField) {
                    JTextField tx = (JTextField) a;
                    tx.setText("0,00");
                }
            }
        }

    }

    public void somaCampos(JPanel pNm, JTextField tNm) {
        double soma = 0;
        for (Component c : pNm.getComponents()) {
            if (c instanceof JTextField) {
                JTextField tx = (JTextField) c;
                soma += Double.parseDouble(retiraVirg(tx.getText()));
            }
        }

        tNm.setText(new DecimalFormat("#.00").format(soma));

    }

    public String retiraVirg(String vlr) {
        vlr = vlr.replace(",", ".");
        return vlr;
    }

    public String colocaVirg(String vlr) {
        vlr = vlr.replace(".", ",");
        return vlr;
    }
}
